/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 *
 * @author yaoyuming
 *
 * @date 2019-11-29 15:47
 */
package com.yao.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.yao.constant.Const;
import com.yao.constant.ParamConst;
import com.yao.core.Table;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Arrays;

/**
 * ftl 生成 java文件工具类
 *
 * @author yaoyuming
 * @date 2019/11/29 15:47
 */
@Slf4j
public class FreemarkerUtil {


    /**
     * 功能描述 : Freemarker模版输出文件
     * 
     * @param tempFile
     *            模版文件
     * @param outFile
     *            输出文件
     * @param table
     *            模版数据对象
     * @author yaoyuming
     * @date 2020/8/5 0005
     */
    public static void generateCode(String tempFile, String outFile, Table table){

        log.info("自定义参数:{} ;;; 模版文件: {} ;;; 输出文件: {}", table.getMap(), tempFile ,outFile);
        // 删除旧文件
        File file = FileUtil.newFile(outFile);
        FileUtil.del(file);

        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig(Const.FTL_DIR, TemplateConfig.ResourceMode.CLASSPATH));
        Template template = engine.getTemplate(tempFile);
        String render = template.render(Dict.create().parseBean(table));
        // 生成新文件
        FileUtil.appendUtf8String(render, file);
    }

    /**
      * 功能描述 : 生成代码
      * @param table
      * @author yaoyuming
      * @date 2020/8/5 0005
      */
    public static void generateCode(Table table){
        // entity
        generateCode("entity_template.ftl", outFile(table.getTableName(), ParamConst.ENTITY_PKG), table);

        // dto
        generateCode("dto_template.ftl", outFile(table.getTableName(), ParamConst.DTO_PKG), table);

        // mapper.xml
        generateCode("mapper_sql_template.ftl", outFile(table.getTableName(), ParamConst.MAPPER_XML_PKG), table);

        // mapper
        generateCode("mapper_template.ftl", outFile(table.getTableName(), ParamConst.MAPPER_PKG), table);

        // Service
        generateCode("service_template.ftl",  outFile(table.getTableName(), ParamConst.SERVICE_PKG), table);

        // ServiceImpl
        generateCode("service_impl_template.ftl",  outFile(table.getTableName(), ParamConst.SERVICE_IMPL_PKG), table);

        // controller
        generateCode("controller_template.ftl",  outFile(table.getTableName(), ParamConst.CONTROLLER_PKG), table);
    }

    
    private  static String outFile(String tableName, String category){
        String destFile = CustMapUtil.get(ParamConst.DEST_PATH);

        String[] tableNames = StrUtil.split(tableName, "_"); // sys_user_role_rel
        String module = tableNames[0];  // sys
        String className = Arrays.asList(tableNames).stream().skip(1)   //UserRoleRel
                .reduce("", (s1, s2) -> StrUtil.concat(true, StrUtil.upperFirst(s1), StrUtil.upperFirst(s2)));

        return destFile + "/temp/" + module + category + StringPool.SLASH + className + ParamConst.pkg.get(category)
                + (ParamConst.MAPPER_XML_PKG.equals(category) ? StringPool.DOT_XML : StringPool.DOT_JAVA);
    }


}